﻿using EasyDDD.AdminModule.Application.CommandsSysAdmin;
using EasyDDD.AdminModule.Application.QueriesSysAdmin;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace EasyDDD.AdminModule.Host.WebAPI.Controllers
{
    /// <summary>
    /// 系统用户管理
    /// </summary>
    /// <param name="_mediator"></param>
    /// <param name="_mapper"></param>
    [Route("api/AdminModule/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class SysAdminController(IMediator _mediator, IMapper _mapper) : ControllerBase
    {
        /// <summary>
        /// 获取用户分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取用户分页列表")]
        [EndpointDescription("AdminModule.SysAdmin.GetPageList")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.GetPageList")]
        public async Task<Dto.SysAdminGetPageListResult> GetPageList([FromQuery] SysAdminGetPageListQuery query)
        {
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminGetPageListResult>(list);
        }


        /// <summary>
        /// 获取用户角色ID列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        [EndpointSummary("获取用户角色ID列表")]
        public async Task<List<Guid>> GetUserRoleIds([FromQuery] SysAdminGetRoleIdsQurery query)
        {
            var list = await _mediator.Send(query, HttpContext.RequestAborted);
            return _mapper.Map<List<Guid>>(list);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [EndpointSummary("添加用户")]
        [EndpointDescription("AdminModule.SysAdmin.Add")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.Add")]
        public async Task<Dto.SysAdminCreateResult> Create([FromBody] SysAdminCreateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminCreateResult>(entity);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("更新用户")]
        [EndpointDescription("AdminModule.SysAdmin.Update")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.Update")]
        public async Task<Dto.SysAdminUpdateResult> Update([FromBody] SysAdminUpdateCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminUpdateResult>(entity);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        [EndpointSummary("删除用户")]
        [EndpointDescription("AdminModule.SysAdmin.Delete")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.Delete")]
        public Task Delete([FromBody] SysAdminDeleteCommand command)
        {
            return _mediator.Send(command, HttpContext.RequestAborted);
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("设置用户角色")]
        [EndpointDescription("AdminModule.SysAdmin.SetRole")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.SetRole")]
        public async Task<Dto.SysAdminSetRoleResult> SetRole([FromBody] SysAdminSetRoleCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<Dto.SysAdminSetRoleResult>(entity);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [EndpointSummary("登录")]
        public async Task<SysAdminLoginCommandResult> Login([FromBody] SysAdminLoginCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return entity;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        [EndpointSummary("重置密码")]
        [EndpointDescription("AdminModule.SysAdmin.ResetPassword")]
        [AuthorizeClaim(ClaimTypes.MenuPermissions, "AdminModule.SysAdmin.ResetPassword")]
        public async Task<string> ResetPassword([FromBody] SysAdminResetPasswordCommand command)
        {
            var entity = await _mediator.Send(command, HttpContext.RequestAborted);
            return _mapper.Map<string>(entity);
        }

    }
}
